package com.dcits.electronic_account.controller;

import com.dcits.electronic_account.bean.ResponseJson;
import com.dcits.electronic_account.bean.VirtualAccount;
import com.dcits.electronic_account.constant.Constant;
import com.dcits.electronic_account.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;

/**
 * @Author: CZJ
 * @Date： 2022/6/24
 */

@RestController
public class LoginController {

    @Autowired
    LoginService loginService;

    /**
     * @func 使用用户名和登录密码进行登录
     * @param virtualAccount
     * @return
     */
    @PostMapping("/loginByUserName")
    public ResponseJson loginByUserName(@RequestBody VirtualAccount virtualAccount){
        HashMap<String, Object> result = loginService.loginByUserName(virtualAccount);
        if (result != null) return new ResponseJson(Constant.SUCCESS_CODE, result);
        return new ResponseJson(Constant.FAIL_CODE, "登录失败！用户名或密码不正确！");
    }

    /**
     * @func 使用手机号和验证码进行登录
     * @param virtualAccount
     * @return
     */
    @PostMapping("/loginByPhone")
    public ResponseJson loginByPhone(@RequestBody VirtualAccount virtualAccount){
        HashMap<String, Object> result = loginService.loginByPhone(virtualAccount);
        if (result != null) return new ResponseJson(Constant.SUCCESS_CODE, result);
        return new ResponseJson(Constant.FAIL_CODE, "登录失败!验证码不正确！");
    }

    /**
     * @func 获取验证码（控制台）
     * @param virtualAccount
     * @return
     */
    @PostMapping("/getVerificationCode")
    public ResponseJson getVerificationCode(@RequestBody VirtualAccount virtualAccount){
        boolean result = loginService.getVerificationCode(virtualAccount);
        if (result) return new ResponseJson(Constant.SUCCESS_CODE, "验证码发送成功，请查看控制台！");
        return new ResponseJson(Constant.FAIL_CODE, "验证码发送失败，请60秒后再尝试！");
    }

    /**
     * @func 查询用户最后一次登录时间
     * @param virtualAccount
     * @return
     * @throws IOException
     */
    @PostMapping("/getLastLoginTime")
    public ResponseJson getLastLoginTime(@RequestBody VirtualAccount virtualAccount){
        HashMap<String, String> result = new HashMap<>();
        String lastLoginTime = loginService.getLastLoginTime(virtualAccount);
        result.put("userId", virtualAccount.getUserId().toString());
        result.put("lastLoginTime", lastLoginTime);
        return new ResponseJson(Constant.SUCCESS_CODE, result);
    }

    /**
     * @func 用户注册
     * @param virtualAccount
     * @return
     */
    @PostMapping("/register")
    public ResponseJson register(@RequestBody VirtualAccount virtualAccount){
        HashMap<Boolean, String> result = loginService.register(virtualAccount);
        if (result.containsKey(true)) return new ResponseJson(Constant.SUCCESS_CODE, "注册成功!");
        return new ResponseJson(Constant.FAIL_CODE, result.values());
    }
}
